# A window that allows a user to enter in new items

import wx

class rapidEntryFrame(wx.Frame):
	def __init__(self, parent, headerString, variableList, boolList, *args, **kwds):
		# Save the header and parent for later
		self.parent = parent
		self.headerString = headerString
		
		# get the basics in place
		kwds["style"] = wx.DEFAULT_FRAME_STYLE
		wx.Frame.__init__(self, *args, **kwds)
		self.backgroundPannel = wx.Panel(self, -1)
		self.headerLabel = wx.StaticText(self.backgroundPannel, -1, self.headerString)
		self.submitButton = wx.Button(self.backgroundPannel, -1, "Submit")
		
		# Create some lists that we can put widgets into later
		self.windowLabels = []
		self.windowCheckDic = {}
		self.windowChecks = []
		self.windowFieldDic = {}
		self.windowFields = []	
		
		for field in variableList:
			self.windowLabels.append(wx.StaticText(self.backgroundPannel, -1, field))
			newTextCtrl = wx.TextCtrl(self.backgroundPannel, -1, field, size=wx.Size(200, 30))
			self.windowFieldDic[field] = newTextCtrl
			self.windowFields.append(newTextCtrl)
		
		for boolField in boolList:
			newCheckBox = wx.CheckBox(self.backgroundPannel, -1, boolField)
			newCheckBox.Bind(wx.EVT_KEY_DOWN, self.toggleCheck)
			self.windowCheckDic[boolField] = newCheckBox
			self.windowChecks.append(newCheckBox)
			
		# Bind the return key and button to tell the parent the user has finished with the item
		self.submitButton.Bind(wx.EVT_KEY_DOWN, self.onKeyDown)
		self.submitButton.Bind(wx.EVT_BUTTON, self.onClick)
		self.Bind(wx.EVT_CLOSE, self.parent.enableRapidEntryButtons)
			
		self.__set_properties()
		self.__do_layout()

	def __set_properties(self):
		# Basically this makes a header and puts in the title
		self.SetTitle(self.headerString)
		self.headerLabel.SetFont(wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
		
	def __do_layout(self):
		# Put in the known layout
		containerSizer = wx.BoxSizer(wx.VERTICAL)
		mainSizer = wx.FlexGridSizer(2, 1, 0, 0)
		
		# Make a grid sizer with the rows equalling the number of fields
		entrySizer = wx.GridSizer(len(self.windowLabels)+int(round(len(self.windowChecks)/2.0)), 2, 0, 0)
		
		# Add the header
		mainSizer.Add(self.headerLabel, 0, 0, 0)
		
		# Put in the fields
		for i in range(0, len(self.windowFields)):
			entrySizer.Add(self.windowLabels[i], 0, 0, 0)
			entrySizer.Add(self.windowFields[i], 0, 0, 0)
		
		for i in range(0, len(self.windowChecks)):
			entrySizer.Add(self.windowChecks[i], 0, 0, 0)
		
		# Finish it off
		mainSizer.Add(entrySizer, 1, wx.EXPAND, 0)
		mainSizer.Add(self.submitButton, 1, wx.EXPAND, 0)
		self.backgroundPannel.SetSizer(mainSizer)
		containerSizer.Add(self.backgroundPannel, 1, wx.EXPAND, 0)
		self.SetSizer(containerSizer)
		containerSizer.Fit(self)
		self.Layout()
	
	def toggleCheck(self, event):
		print "toggle check"
		key = event.GetKeyCode()
		if key == wx.WXK_SPACE:
			callingObject = event.GetEventObject()
			if callingObject.IsChecked(): callingObject.SetValue(False)
			else: callingObject.SetValue(True)
			self.Navigate(wx.NavigationKeyEvent.IsForward)
		elif key == wx.WXK_TAB:
			print "manual tab"
			self.Navigate(wx.NavigationKeyEvent.IsForward)
			event.Skip()
	
	def onKeyDown(self, event): # Check to see if it is the right key
		print "checking key"
		key = event.GetKeyCode()
		if key == wx.WXK_RETURN:
			self.windowFields[0].SetFocus()
			self.parent.addNewRapidEntry(event)
			for field in self.windowFields: field.SetValue("")
			for check in self.windowChecks: check.SetValue(False)
		event.Skip()
	
	def onClick(self, event): # Send it to the parent
		self.windowFields[0].SetFocus()
		self.parent.addNewRapidEntry(event)
		for field in self.windowFields: field.SetValue("")
		for check in self.windowChecks: field.SetValue("")
		event.Skip()

def createRapidEntryWindow(parent, parentWindow, header, fields, boolFields=[]):
	rapidEntryWindow = rapidEntryFrame(parent, header, fields, boolFields, parentWindow, -1, "")
	rapidEntryWindow.Show()
	return rapidEntryWindow
